<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/inc/common.jsp"%>
<html>
<head>
<title>后台管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<div class="easyui-layout" fit="true">
		<div data-options="region:'west',split:true,title:'资源清单'"  style="width:20%;">
			<!-- 左侧资源树 -->
			<ul id="tree" class="ztree" style="width:auto; overflow:auto;"></ul>
		</div>
		<!-- 数据列表 -->
		<div data-options="region:'center',title:'资源信息',toolbar:'#searchtoolbar',footer:'#searchfooter'" >
			<!-- 新增按钮 -->
			<div id="searchtoolbar" class="datagrid-toolbar">
					<a id="ct-add" onclick="addData()" class="easyui-linkbutton" icon="icon-add" plain="true" href="javascript:void(0)">新增</a>
					<span class="search-btn-separator"></span>
					<a id="ct-delete" onclick="deleteData()" class="easyui-linkbutton" icon="icon-delete" plain="true" href="javascript:void(0)">删除</a>
			</div>
			<div style="padding:2px 2px;">
				<form id="dataFrom" class="easyui-form" method="post">
				<input type="hidden" id="id" name="id">
                <input type="hidden" id="resLevel" name="resLevel">
			    	<table  style="width:100%;" cellpadding="0" cellspacing="1"  class="formtablecol2">
			    		<tr class="table-tr">
			    			<td align="right" style="width: 10%">名称:</td>
			    			<td class="value">
				    			<input class="easyui-textbox" id="name" name="name" data-options="required:true,validType:'maxLength[30]'"></input>
			    			</td>
			    			<td align="right" style="width: 10%">上级资源:</td>
			    			<td class="value">
			    				<input type="hidden" id="parentId" name="parentId">
			    				<input class="easyui-textbox" id="parentName" name="parentName" data-options="editable:false"></input>
			    			</td>
			    		</tr>
			    		<tr>
			    			<td align="right" >类型:</td>
			    			<td class="value">
			    				<input class="easyui-combobox" id="resType" name="resType"  data-options="url:'<%=path%>/sys/dictionary/dicbykey.do?key=1003&type=1',method:'get',valueField:'dataCode',textField:'dataName',panelHeight:'auto',editable:false,required:true">
			    			</td>
			    		
			    			<td align="right" >图标:</td>
			    			<td class="value">
			    				<input class="easyui-combobox" id="icon" name="icon"  data-options="url:'<%=path%>/sys/dictionary/dicbykey.do?key=1004&type=1',method:'get',valueField:'dataCode',textField:'dataName',panelHeight:'142px;',editable:false,required:true">
			    			</td>
			    		</tr>
			    		<tr>
			    			<td align="right" >状态:</td>
			    			<td class="value">
			    				<input class="easyui-combobox" id="state" name="state"  data-options="url:'<%=path%>/sys/dictionary/dicbykey.do?key=1001&type=1',method:'get',valueField:'dataCode',textField:'dataName',panelHeight:'auto',editable:false,required:true">
			    			</td>
			    			<td align="right" >排序:</td>
			    			<td class="value">
			    				<input id="sort" name="sort" class="easyui-numberbox" data-options="min:1,max:99"></input>
			    			</td>
			    		</tr>
			    		<tr>
			    			<td align="right" >地址:</td>
			    			<td class="value" colspan="3">
			    				<input class="easyui-textbox" id="resUrl" name="resUrl" data-options="validType:'maxLength[120]'" style="width: 350px;"></input>
			    			</td>
			    		</tr>
			    		<tr>
			    			<td align="right" >备注:</td>
			    			<td class="value" colspan="3">
			    				<input id="description" name="description" class="easyui-textbox" data-options="multiline:true" data-options="validType:'maxLength[150]'" style="width: 350px;height: 60px;"></input>
			    			</td>
			    		</tr>
			    	</table>
			    </form>
		    </div>
			<!-- 保存按钮 -->
			<div id="searchfooter" style="text-align:right;padding: 0px 10px">
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="saveDate()"  data-options="iconCls:'icon-page_save',plain:true" >保存</a>
				<span class="search-btn-separator"></span>
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearDate()" data-options="iconCls:'icon-undo',plain:true">重置</a>
			</div>
		</div>
	</div>
</body>
</html>
<script type="text/javascript">
	$(function() {
		initTree();//初始化树
		$('#ct-delete').linkbutton('disable');
	});
	
	//初始化树结构
	function initTree(){
		var setting = {
			check:{
		    	enable:false,//是否可勾选，只有设置成ture其他才生效
		        chkStyle: "radio",//勾选框类型
				radioType: "all"//radio 的分组范围
		    },
		    view: {
		        dblClickExpand: false,//双击节点时，是否自动展开父节点的标识
		        showLine: true,//是否显示节点之间的连线。
		        selectedMulti: false//是否允许同时选中多个节点。
		    },
		    data: {
		        simpleData: {
		        	enable:true,
		            idKey: "id",
		            pIdKey: "parentId",
		            rootPId: "0"
		        }
		    },
		    callback: {
		   		onClick: zTreeOnClick
		    }
		};
		
		$.ajax({
			type: "post",
			url: "${basePath}/sys/resources/query.do",
			dataType:"json",
			success:function(data){
				$.fn.zTree.init($("#tree"), setting, data);
			}
		});
	}
	
	//获取点击菜单信息
	function zTreeOnClick(event, treeId, treeNode){
		//选择根节点
		if(treeNode.id == 0){
			loadData(null,null);
		}else{
			$.ajax({
				type: "post",
				url: "${basePath}/sys/resources/edit.do",
				data: "id="+treeNode.id,
				dataType:"json",
				success:function(data){
					$.each(data,function(i,n){
						loadData(n.resources,treeNode);
					});
				}
			});
		}
		
	}
	
	//加载菜单数据
	function loadData(data,treeNode){
		//判断是否为根节点
		if(data == null && treeNode == null){
			$('#dataFrom').form('clear');//清空数据
			$("#id").val(0);//设置当前节点为根节点
			$('#ct-add').linkbutton('enable');
			$('#ct-delete').linkbutton('disable');
		}else{
			//页面元素赋值
			$("#id").val(data.id);
			$("#name").textbox('setValue',data.name);
			$("#parentId").val(treeNode.getParentNode().id);
			$("#parentName").textbox('setValue',treeNode.getParentNode().name);
			$("#sort").textbox('setValue',data.sort);
			$("#resUrl").textbox('setValue',data.resUrl);
			$("#description").textbox('setValue',data.description);
			$('#resType').combobox('setValue',data.resType);
			$('#icon').combobox('setValue',data.icon);
			$('#state').combobox('setValue',data.state);
			
			//设置类型为可编辑
			$('#resType').combobox('enable');
			//一级菜单
			if(data.resLevel == 1){
				$('#ct-add').linkbutton('enable');
				$('#ct-delete').linkbutton('enable');	
				//判断是否有子节点
				if(treeNode.children.length > 0){
					$('#ct-delete').linkbutton('disable');
					$('#resType').combobox('disable');
				}
			}else{
				//二级菜单
				$('#ct-add').linkbutton('disable');
				$('#ct-delete').linkbutton('enable');
			}	
		}
	}
	
	//新增
	function addData(){
		var id = $("#id").val();
		var name = $("#name").textbox('getValue');
		$('#dataFrom').form('clear');//清空数据
		if(id == 0){
			$('#resType').combobox('enable');
			$("#parentName").textbox('setValue','资源列表');
			$('#resType').combobox('setValue','100301');//设置默认为模块
			$('#icon').combobox('setValue','icon-nav');
			$("#resUrl").textbox('setValue','#');
			$("#resLevel").val('1');
		}else{
			$("#parentName").textbox('setValue',name);
			$('#resType').combobox('setValue','100302');//设置默认为菜单
			$('#icon').combobox('setValue','icon-nav');
			$("#resLevel").val('2');
			
		}
		$("#parentId").val(id);
		$('#state').combobox('setValue','100101');
	}
	
	//删除
	function deleteData(){
		var id = $("#id").val();
		$.ajax({
            url:'${basePath}/sys/resources/delete.do',  
            type:'post',         
            data:"id="+id, 
            dataType:"json",
            success:function(msg){
            	$.each(msg,function(i,n){
            		if(n.message == "success"){
            			$.messager.alert('消息', "删除成功!");
            			clearDate();
            			initTree();
            		}else{
            			$.messager.alert('消息', '失败!请重新操作!','warning');
            		}
               });
                 
           	}
         });
	}
	
	//保存
	function saveDate(){
		var parentId = $("#parentId").val();
		if(parentId == ""){
			$.messager.alert('消息', "点击菜单列表后必须点击新增按钮!");
		}
		var actionUrl = "${basePath}/sys/resources/save.do";
		$('#dataFrom').form('submit', {
			url:actionUrl,
			onSubmit:function(){
				return $(this).form('validate');
			},
		    success:function(msg){
	    		if(msg == "success"){
	    			$.messager.alert('消息', "保存成功!");
	    			clearDate();
         			initTree();
		        }else{
		            $.messager.alert('消息', "系统异常,请联系管理员!");
		        }	  
		    }   
		 });
	}
	
	function clearDate(){
		$('#dataFrom').form('clear');
		$('#resType').combobox('setValue','100301');//设置默认为模块
		$('#icon').combobox('setValue','icon-nav');
		$('#state').combobox('setValue','100101');
	}
	
</script>

